package com.wnhz.work.jy.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.wnhz.work.jy.BaseActivity;
import com.wnhz.work.jy.MyApplication;
import com.wnhz.work.jy.R;
import com.wnhz.work.jy.utils.ConfigUtil;
import com.wnhz.work.jy.utils.xutils3.MyCallBack;
import com.wnhz.work.jy.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 修改昵称
 */
public class NicknameActivity extends BaseActivity implements View.OnClickListener {
    private TextView title;
    private EditText et_name;

    private String oldName;
    private TextView view_main_right_text;
    private View btn_main_right;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nickname);
        initData();
        initView();
    }

    private void initData() {
        oldName = getIntent().getStringExtra("oldName");
    }

    private void initView() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("修改昵称");
        view_main_right_text = (TextView) findViewById(R.id.view_main_right_text);
        view_main_right_text.setTextColor(0xffe2bb0a);
        view_main_right_text.setVisibility(View.VISIBLE);
        view_main_right_text.setText("完成");
        btn_main_right = findViewById(R.id.btn_main_right);
        btn_main_right.setVisibility(View.VISIBLE);
        btn_main_right.setOnClickListener(this);
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        findViewById(R.id.tv_true).setOnClickListener(this);
        findViewById(R.id.tv_true).setVisibility(View.GONE);

        et_name = (EditText) findViewById(R.id.et_name);
        et_name.setText(oldName);

        //将光标放到文字最后
        et_name.setSelection(et_name.getText().length());

        //弹出软键盘
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {

                           public void run() {
                               InputMethodManager inputManager =
                                       (InputMethodManager) et_name.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                               inputManager.showSoftInput(et_name, 0);
                           }

                       },
                300);

        et_name.setFilters(new InputFilter[]{new InputFilter.LengthFilter(16)});//设置最多字数
        et_name.addTextChangedListener(mTextWatcher);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;

            //确定
            case R.id.btn_main_right:
                setuserName();

               /* //数据是使用Intent返回
                Intent intent = new Intent();

                //把返回数据存入Intent
                intent.putExtra("newName", et_name.getText().toString());

                //设置返回数据
                NicknameActivity.this.setResult(RESULT_OK, intent);

                //关闭Activity
                NicknameActivity.this.finish();*/
                break;

            default:
                break;
        }
    }

    TextWatcher mTextWatcher = new TextWatcher() {
        private CharSequence temp;
        private int editStart;
        private int editEnd;

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub
            temp = s;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
            // TODO Auto-generated method stub
//          mTextView.setText(s);//将输入的内容实时显示
        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
            editStart = et_name.getSelectionStart();
            editEnd = et_name.getSelectionEnd();
            if (temp.length() > 15) {
                Toast.makeText(NicknameActivity.this, "昵称最多输入15个字！", Toast.LENGTH_SHORT).show();
                s.delete(editStart - 1, editEnd);
                int tempSelection = editStart;
                et_name.setText(s);
                et_name.setSelection(tempSelection);
            }
        }
    };

    private static final String TAG = "NicknameActivity";
    private void setuserName (){
        final String name = et_name.getText().toString();
        Map<String,Object> map = new HashMap<>();
        map.put("userId", MyApplication.getInstance().userBean.getUserId());
        map.put("userName",name);
        showDialog();
        XUtil.Post(ConfigUtil.MODIFY_NICKNAME,map,new MyCallBack<String>(){
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.e(TAG, "onSuccess: "+result );
                try {
                    JSONObject json = new JSONObject(result);
                    String re = json.optString("result");
                    String info = json.optString("info");
                    Toast.makeText(getApplication(),info,Toast.LENGTH_SHORT).show();
                    if ("1".equals(re)){
                        MyApplication.getInstance().userBean.setUserName(name);
                        finish();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
                closeDialog();
            }
        });
    }

}
